package com.example.appkcsj.slice;

import com.example.appkcsj.ResourceTable;
import com.example.appkcsj.dbutils.DBUtils;
import com.example.appkcsj.entity.Student;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class mupdateAbilitySlice extends AbilitySlice {
    private final static HiLogLabel HI_LOG_LABEL=
            new HiLogLabel(HiLog.LOG_APP,0x00201,"mupdateAbilitySlice");
    private DBUtils dbUtils;
    private Button xiugai;
    private TextField userna1;
    private TextField newpass1;
    private TextField newpass2;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_mupdate);
        dbUtils=new DBUtils(mupdateAbilitySlice.this);
        xiugai= (Button) findComponentById(ResourceTable.Id_xiugai1);
        userna1= (TextField) findComponentById(ResourceTable.Id_userna1);
        newpass1= (TextField) findComponentById(ResourceTable.Id_newpass1);
        newpass2= (TextField) findComponentById(ResourceTable.Id_newpass2);
        if(intent!=null){
            String name=intent.getStringParam("username1");
            String pass1=intent.getStringParam("password1");
            String pass2=intent.getStringParam("password2");
            Student student=new Student(name,pass1,pass2);
            dbUtils.insertStudent(student);
        }
        xiugai.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Student student=dbUtils.queryOneStudent();
                if("".equals(userna1.getText())||"".equals(newpass1.getText()))
                {
                    ToastDialog td = new ToastDialog(mupdateAbilitySlice.this);
                    td.setText("请输入用户名及密码！");
                    td.show();
                }
                else{
                    if (newpass2.getText().equals(newpass1.getText())) {
                        student.setPassword1(newpass1.getText());
                        student.setPassword1(newpass1.getText());
                        student.setPassword2(newpass2.getText());
                        boolean flag=dbUtils.updateStudent(student);
                        if(flag){
                            Intent m = new Intent();
                            m.setParam("username1",userna1.getText());
                            m.setParam("password1",newpass1.getText());
                            m.setParam("password2",newpass2.getText());
                            present(new MainAbilitySlice(),m);
                            Operation operation = new Intent.OperationBuilder()
                                    .withDeviceId("")
                                    .withBundleName("com.example.appkcsj")//跳转到哪个应用
                                    .withAbilityName("com.example.appkcsj.sixthAbility")//要跳转的包名
                                    .build();//将上面的信息打包
                            m.setOperation(operation);
                            ToastDialog td = new ToastDialog(mupdateAbilitySlice.this);
                            td.setText("修改成功！");
                            td.show();
                        }
                        else{
                            ToastDialog td = new ToastDialog(mupdateAbilitySlice.this);
                            td.setText("修改失败！");
                            td.show();
                        }
                    }
                    else {
                        if("".equals(newpass2.getText())){
                            ToastDialog td = new ToastDialog(mupdateAbilitySlice.this);
                            td.setText("请确认密码！");
                            td.show();
                        }
                        else{
                            ToastDialog td = new ToastDialog(mupdateAbilitySlice.this);
                            td.setText("两次密码不一致！");
                            td.show();}
                    }
                }

            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

